﻿#include "stdafx.h"
#include "SimpleList.h"




///////////////////////////////////////////////////////////////////////

int SimpleList::getLength(){
	return length;
}

bool SimpleList::hasNext(){
	return (length > curIterator) ? true : false;
}

SimpleList::SimpleList()
{
}
SimpleList::~SimpleList()
{
}

///////////////////////////////////////////////////////////////////////


void SimpleList::append(int element){
	

	SimpleListElement*  myElement = new SimpleListElement;
	
	myElement -> value = element;

	if (length == 0 ){
	
		firstAdress = myElement;	
		lastAdress = myElement;
		myElement -> nextAdress = myElement;
		currIteratorAdress = myElement;
	}
	else{
				
		SimpleListElement* myTemp = lastAdress;
		myTemp ->nextAdress  = myElement;		

		myElement->prevAdress = lastAdress;

		lastAdress = myElement;
	
	}

	length++;
}



int SimpleList::getValue(){

tempCur = *currIteratorAdress;
if (  tempCur.nextAdress != NULL)
currIteratorAdress = tempCur.nextAdress;
curIterator++;

	return tempCur.value;

}



int SimpleList::getValueByNumberOrderByBeginning(int number){

	currIteratorAdress = firstAdress;

	for (int i = 0; i < number; i++){

		tempCur = *currIteratorAdress;
		if (tempCur.nextAdress != NULL) currIteratorAdress = tempCur.nextAdress;

		curIterator++;

}
		currIteratorAdress = firstAdress;

		return tempCur.value;


}


int SimpleList::getValueByNumberOrderByEnd(int number){

	currIteratorAdress = lastAdress;

	for (int i = length; i > length - number; i--){

		tempCur = *currIteratorAdress;
		if (tempCur.prevAdress != NULL) currIteratorAdress = tempCur.prevAdress;

		curIterator++;

	}
	currIteratorAdress = firstAdress;

	return tempCur.value;


}

void SimpleList::pop(int number){

	
	currIteratorAdress = firstAdress;

	for (int i = 1; i < number+2; i++){

		
		tempCur = *currIteratorAdress;

		if (i == number -1) { forDeletingPrevElement = currIteratorAdress; }
		if (i == (number+1) ) forDeletingNextElement = currIteratorAdress;
if (tempCur.nextAdress != NULL) currIteratorAdress = tempCur.nextAdress;
		
	}

	// Magic :)
	SimpleListElement* myTempDel = forDeletingPrevElement;
	myTempDel->nextAdress = forDeletingNextElement;


	SimpleListElement* myTempDel2 = myTempDel->nextAdress;
	myTempDel2->prevAdress = forDeletingPrevElement;




	int result = myTempDel -> value;
	currIteratorAdress = firstAdress;

	length --;
	
}


void SimpleList::appendAfter(int element,int position){
	currIteratorAdress = firstAdress;//
	position++;

	SimpleListElement*  myElement = new SimpleListElement;

	myElement->value = element;

	if (length > 0){

//////////////////// MAGIC ///////////
		for (int i = 1; i < position + 2; i++){

			
			tempCur = *currIteratorAdress;

			if (i == position - 1) { forDeletingPrevElement = currIteratorAdress; }

			if (i == (position )) forDeletingNextElement = currIteratorAdress;

			if (tempCur.nextAdress != NULL) currIteratorAdress = tempCur.nextAdress;



		}

		// Magic :)
		SimpleListElement* myTempDel = forDeletingPrevElement;
		myTempDel->nextAdress = myElement;

		SimpleListElement* myTempDel2 = forDeletingNextElement;
		myTempDel2->prevAdress = myElement;

		myElement->prevAdress = myTempDel;
		myElement->nextAdress = myTempDel2;
		
length++;
	}

	
}
